package com.meishij.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 阿飞 on 2016/10/14.
 */

public class ViewPagerAdapter extends PagerAdapter {

    private Context context;
    private List<View> views;

    public ViewPagerAdapter(Context context, List<View> views) {
        this.context = context;
        this.views = views;
        if (views == null) {
            this.views = new ArrayList<>();
        }
    }

    public void add(View view) {
        this.views.add(view);
        notifyDataSetChanged();
    }

    public void addALl(List<View> views) {
        this.views.addAll(views);
        notifyDataSetChanged();
    }

    public void clear() {
        this.views.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return views == null ? 0 : views.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = views.get(position);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
